import {
  getMsgDetail,
  supplementSheet
} from "../../utils/config"
import store from "../../utils/factory"
const util = require('../../utils/util.js')

const app = getApp()

let that = null;

Page({
  data: {
    detail: {},
    quantity: 0,
    remark: "",
    unitPrice: 0,
    total: 0
  },
  onLoad: function (e) {
    that = this
    // console.log(e.id)
    this.getOrderDetail(e.id)
  },
  onShow() {

  },
  submitOrder() {
    if (this.data.submitStatus) {
      return false
    }
    this.setData({
      submitStatus: true
    })
    wx.showLoading({
      title: "提交补单中"
    })
    supplementSheet({
      id: this.data.detail.id,
      remark: this.data.remark,
      quantity: this.data.quantity
    }).then(res => {
      wx.hideLoading()
      that.setData({
        submitStatus: false
      })
      wx.showModal({
        title: "温馨提示",
        content: "临时补单成功",
        showCancel: false, //是否显示取消按钮
        success: function (res) {
          wx.navigateBack({
            delta: 1
          });
        }
      });
    }).catch(err => {
      wx.hideLoading()
    })
  },
  // 减去
  bindMinus(e) {
    if (this.data.quantity == 1) {
      wx.showToast({
        icon: 'none',
        title: "商品数量最少不小于1"
      })
      return false
    }
    let quantity = util.accAdd(this.data.quantity, -1);
    let a = this.data.unitPrice;
    let total = util.accAdd(this.data.total, -a)
    this.setData({
      quantity: quantity,
      total: total
    })
  },
  // 输入
  bindManual(e) {
    if (e.detail.value == 0) {
      wx.showToast({
        icon: 'none',
        title: "商品数量最少不小于1"
      })
      this.setData({
        quantity: 1,
        total: this.data.unitPrice
      })
      return false
    }
    this.setData({
      quantity: e.detail.value,
      total: util.accMul(e.detail.value, this.data.unitPrice)
    })
  },
  // 加法
  bindPlus(e) {
    let quantity = util.accAdd(this.data.quantity, 1);
    let total = util.accAdd(this.data.total, this.data.unitPrice)
    this.setData({
      quantity: quantity,
      total: total
    })
  },
  watchRemark(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  getOrderDetail(id) {
    getMsgDetail({
      id: id
    }).then(res => {
      let req = res.data;
      req.createTime = util.formatTime(new Date(req.createTime));
      req.unitPrice = (req.cost / req.quantity).toFixed(2)
      this.setData({
        detail: req,
        quantity: req.quantity,
        remark: req.remark,
        unitPrice: req.unitPrice,
        total: req.cost
      })
    })
  }
})